package com.cuit.jifver.weibo.activity;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import android.app.Dialog;
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ViewTreeObserver.OnScrollChangedListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;

import com.cuit.jifver.weibo.R;
import com.cuit.jifver.weibo.adapter.ListViewAdapter;
import com.cuit.jifver.weibo.data.Weibo;
import com.cuit.jifver.weibo.model.WeiBoData;
import com.cuit.jifver.weibo.model.WeiBoList;
import com.cuit.jifver.weibo.model.sina.Status;
import com.cuit.jifver.weibo.utils.ConfigUtil;
import com.cuit.jifver.weibo.utils.DialogUtil;
import com.cuit.jifver.weibo.widget.PullToRefreshListView;
import com.cuit.jifver.weibo.widget.PullToRefreshListView.OnRefreshListener;

public class HomeListAct extends ListActivity implements OnScrollListener
{
	 private LinkedList<String> mListItems;
	 private Weibo weibo = Weibo.getInstance();
	 private ListViewAdapter adapter;
	 private String[] mStrings = {
	            "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam",
	            "Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis",
	            "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
	            "Allgauer Emmentaler"};

	 private List<WeiBoData> weiBoDatas = null;
	 
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_home_list);
		
		((PullToRefreshListView) getListView()).setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                // Do work to refresh the list here.
                new GetDataTask().execute();
            }
        });

      //  mListItems = new LinkedList<String>();
       // mListItems.addAll(Arrays.asList(mStrings));

       // ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);

        init();
	}
	
	
	private void init()
	{
		if (ConfigUtil.getInstance().getCurWeibo().equals(ConfigUtil.QQW))
		{
			WeiBoList weiBoList = (WeiBoList) weibo.getHomeTimeLine(this, null);
			if (weiBoList != null)
			{
				if (weiBoList.getErrcode() == 0)
				{
					weiBoDatas = weiBoList.getData().getInfo();
				}
				else 
				{
					DialogUtil.OneButtonShowMessageDialog(this, "Fail");
				}

				
			}
			adapter = new ListViewAdapter(weiBoDatas, this);
			
			setListAdapter(adapter);
		}
		
		if (ConfigUtil.getInstance().getCurWeibo().equals(ConfigUtil.SINAW))
		{
			List<Status> weiBoList = (List<Status>) weibo.getHomeTimeLine(this, null);
			if (weiBoList != null)
			{
				
				adapter = new ListViewAdapter(weiBoList, this);				
				setListAdapter(adapter);
				
			}
			
		}
		
	}
	
	 private class GetDataTask extends AsyncTask<Void, Void, String[]> {

	        @Override
	        protected String[] doInBackground(Void... params) {
	            // Simulates a background job.
	            try {
	                Thread.sleep(2000);
	            } catch (InterruptedException e) {
	                ;
	            }
	            return mStrings;
	        }

	        @Override
	        protected void onPostExecute(String[] result) {
	            //mListItems.addFirst("Added after refresh...");

	            // Call onRefreshComplete when the list has been refreshed.
	            ((PullToRefreshListView) getListView()).onRefreshComplete();

	            super.onPostExecute(result);
	        }
	    }

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState)
	{
		
		
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount)
	{
	
		
	}

	 
	    
}
